/* @flow */

import { parse } from './parser/index'
import { optimize } from './optimizer'
import { generate } from './codegen/index'
import { createCompilerCreator } from './create-compiler'

// `createCompilerCreator` allows creating compilers that use alternative
// parser/optimizer/codegen, e.g the SSR optimizing compiler.
// Here we just export a default compiler using the default parts.

// baseCompile：接收模板和合并后的options选项参数
// 1.把模板编译成抽象语法树(AST)
// 2.优化抽象语法树
// 3.把抽象语法树转换为字符串形式的js代码
export const createCompiler = createCompilerCreator(function baseCompile(
    template: string,
    options: CompilerOptions
): CompiledResult {
    //把模板转换成ast抽象语法树
    //抽象语法树，用来以树形的方式描述代码结构
    const ast = parse(template.trim(), options)
    if (options.optimize !== false) {
        //优化抽象语法树
        optimize(ast, options)
    }
    //把抽象语法树生成字符串形式的js代码
    const code = generate(ast, options)
    return {
        ast,
        render: code.render,
        staticRenderFns: code.staticRenderFns
    }
})
